package org.ishequ.service;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.ishequ.domain.News;
import org.ishequ.utils.StreamTools;
import org.json.JSONArray;
import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

public class WebService {

	public byte[] getImg(String address) throws Exception {
		URL url = new URL(address);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setReadTimeout(5000);
		conn.setRequestMethod("GET");
		if(conn.getResponseCode() == 200){
			InputStream is = conn.getInputStream();
			return StreamTools.read(is);
		}
		return null;
	}
	
	public String getHtml(String address) throws Exception {
		URL url = new URL(address);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setReadTimeout(5000);
		conn.setRequestMethod("GET");
		if(conn.getResponseCode() == 200){
			InputStream is = conn.getInputStream();
			byte[] data = StreamTools.read(is);
			String html = new String(data,"UTF-8");
			return html;
		}
		return null;
	}
	
	public List<News> getXmlNews(String path) throws Exception{
		URL url = new URL(path);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setReadTimeout(5000);
		conn.setRequestMethod("GET");
		
		List<News> list = new ArrayList<News>();
		News news = null;
		if(conn.getResponseCode() == 200){
			InputStream is = conn.getInputStream();
			XmlPullParser pullParser = XmlPullParserFactory.newInstance().newPullParser();
			pullParser.setInput(is, "UTF-8");
			int event = pullParser.getEventType();
			while(event != XmlPullParser.END_DOCUMENT){
				switch (event) {
				case XmlPullParser.START_TAG:
					if("news".equals(pullParser.getName())){
						news = new News();
						int id = Integer.valueOf(pullParser.getAttributeValue(0));
						news.setId(id);
					}
					if("title".equals(pullParser.getName())){
						String name = pullParser.nextText();
						news.setName(name);
					}
					if("timelength".equals(pullParser.getName())){
						long timelen = Long.valueOf(pullParser.nextText());
						news.setTimelen(timelen);
					}
					break;

				case XmlPullParser.END_TAG:
					if("news".equals(pullParser.getName())){
						list.add(news);
						news = null;
					}
					break;
				}
				event = pullParser.next();
			}
		}
		return list;
	}
	
	
	public List<News> getJsonNews(String path) throws Exception{
		URL url = new URL(path);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setReadTimeout(5000);
		conn.setRequestMethod("GET");
		
		List<News> list = new ArrayList<News>();
		News news = null;
		if(conn.getResponseCode() == 200){
			InputStream in = conn.getInputStream();
			byte[] data = StreamTools.read(in);
			String json = new String(data,"UTF-8");
			JSONArray jsonArray = new JSONArray(json);
			for(int i=0;i<jsonArray.length();i++){
				JSONObject obj = jsonArray.getJSONObject(i);
				int id = Integer.valueOf(obj.getString("id"));
				String name = obj.getString("name");
				long timelen = Long.valueOf(obj.getString("timelen"));
				news = new News(id,name,timelen);
				list.add(news);
				news = null;
			}
		}
		return list;
	}
}
